package com.huadao.cart_ccs.common;

import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.entity.Project;
import com.huadao.cart_ccs.greendao.gen.ProjectDao;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Date;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 实验项目管理类
 */
public class ProjectManager {
    private Project project;
    private ProjectDao projectDao=GreenDaoManager.getInstance().getNoCacheSession().getProjectDao();

    private static final ProjectManager ourInstance = new ProjectManager();

    public static final int STATUE_SUCC=0;
    public static final int STATUE_FAIL=-1;

    public static ProjectManager getInstance() {
        return ourInstance;
    }

    private ProjectManager() {
    }

    public long getProjectId() {
        if(project==null){
            return -1;
        }else {
            return project.getProjectId();
        }
    }

    //获取上一次的项目
    public Project getLastProject() {
        List<Project> projectList=projectDao.queryBuilder().orderDesc(ProjectDao.Properties.StartTime).limit(1).list();
        if(projectList!=null&&projectList.size()==1){
            Project project=projectList.get(0);
            return project;
        }else {
            return null;
        }
    }

    //初始化实验项目
    public void initProject(){
        //先删除之前项目的所有数据,如果数据多的话，会比较耗时
        GreenDaoManager.getInstance().deleteAllData();
        project = new Project(null, new Date(), null, false,false);
        projectDao.insert(project);
        Constants.RawMessagefileName="project_"+project.getProjectId()+"_RawMessage.log";
    }

    /**
     * 恢复实验项目
     * @return   STATUE_FAIL没有可以恢复的项目,STATUE_SUCC恢复项目成功
     */
    public int resumeProject(){
        List<Project> projectList=projectDao.queryBuilder().orderDesc(ProjectDao.Properties.StartTime).list();
        if(projectList!=null&&projectList.size()>0){
            Project project=projectList.get(0);
            if(project.getEndTime()==null) {//这个项目未完成
                this.project=project;
                Constants.RawMessagefileName="project_"+project.getProjectId()+"_RawMessage.log";
                //恢复称重参考值
//                CartApplication.getInstance().setReferenceWeight(ParamsPreferences.getInstance().getReferenceWeight());
                return STATUE_SUCC;
            }else {//这个项目已完成
                return STATUE_FAIL;
            }
        }else {
            return STATUE_FAIL;
        }
    }

    //重新开始一个新项目
    public void restartProject() {
        //先删除之前项目的所有数据
        GreenDaoManager.getInstance().deleteAllData();

        project = new Project(null, new Date(), null, false,false);
        projectDao.insert(project);
        Constants.RawMessagefileName="project_"+project.getProjectId()+"_RawMessage.log";
    }


    public boolean isFinishCollect() {
        return project.getIsFinishCollect();
    }

    public void setFinishCollect(boolean isFinish) {
        project.setIsFinishCollect(isFinish);
        projectDao.update(project);
    }
}
